home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Microsoft / Expression Blend / Blend.en.msi / Sparkle.GPiano.DelayedCallback.cs.en < prev    next >
Encoding:
Text File  |  2007-03-19  |  1.8 KB  |  34 lines

  1.  ■using System;
  2. using System.Threading;
  3. using System.Windows;
  4. using System.Windows.Threading;
  5. namespace GrandPiano
  6. {
  7.     /// <summary>
  8.     /// Solves the problem of scope when using Timer(), since it runs on a separated thread.
  9.     /// </summary>
  10.     public static class DelayedCallback
  11.     {
  12.         public delegate void Callback();
  13.         public static void Invoke(TimeSpan delay, Callback callback)
  14.         {
  15.             new Timer(DelayedCallback.HandleTimeout, callback, delay, TimeSpan.FromMilliseconds(-1));
  16.         }
  17.         private static void HandleTimeout(object state)
  18.         {
  19.             if (Application.Current != null)
  20.             {
  21.                 Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(
  22.                         delegate(object arg)
  23.                         {
  24.                             ((Callback)state)();
  25.                             return null;
  26.                         }), null);
  27.             }
  28.         }
  29.     }
  30. }